package com.ktwlsoft.fundmanage.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.fundmanage.domain.entity.PayInType;
import com.ktwlsoft.fundmanage.domain.repository.PayInTypeRepository;
import com.ktwlsoft.fundmanage.mapper.CurrencyMapper;
import com.ktwlsoft.fundmanage.payload.BaseResult;
import com.ktwlsoft.fundmanage.payload.ResultType;
import com.ktwlsoft.fundmanage.utils.KeyWorkerUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 出账入账类型配置
 *
 * @author Name on 2019/9/9 11:03
 * @version 1.0
 */
@Service
public class PayInTypeService {

    private final PayInTypeRepository payInTypeRepository;

    private final CurrencyMapper currencyMapper;

    public PayInTypeService(PayInTypeRepository payInTypeRepository, CurrencyMapper currencyMapper){
        this.payInTypeRepository = payInTypeRepository;
        this.currencyMapper = currencyMapper;
    }

    /**
     * 出账入账类型配置-分页 on 2019/9/9 11:06
     *
     * @param keyWords keyWords
     * @return BaseResult<?>
     */
    public BaseResult<?> payinTypePage(String payType,String keyWords,int pn,int pageSize){
        JSONObject jsonObject = new JSONObject();
        if(keyWords != null && !"".equals(keyWords.trim())){
            jsonObject.put("keyWords",keyWords);
        }
        if(payType != null && !"".equals(payType)){
            if(payType.equals(PayInType.PayType.PAYIN_FLAG.toString())){
                jsonObject.put("payType",PayInType.PayType.PAYIN_FLAG.toString());
            }
            if(payType.equals(PayInType.PayType.PAYIN_METHOD.toString())){
                jsonObject.put("payType",PayInType.PayType.PAYIN_METHOD.toString());
            }
            if(payType.equals(PayInType.PayType.PAYIN_TYPE.toString())){
                jsonObject.put("payType",PayInType.PayType.PAYIN_TYPE.toString());
            }
            if(payType.equals(PayInType.PayType.FUND_OUT_TYPE.toString())){
                jsonObject.put("payType",PayInType.PayType.FUND_OUT_TYPE.toString());
            }
        }
        PageHelper.startPage(pn,pageSize);
        List<?> list = currencyMapper.payInTypeList(jsonObject);
        return BaseResult.body(new PageInfo<>(list), ResultType.BUSINESS_SUCCESS);
    }

    /**
      * 出账入账类型配置-新增或修改 on 2019/9/9 11:19
      *
      * @param payInType payInType
      * @return BaseResult<?>
      */
    public BaseResult<?> payinTypeSave(PayInType payInType){
        if(payInType.getPayType() == null){
            return BaseResult.body(payInType,ResultType.BUSINESS_ERROR).withMessage("支付类型为空");
        }
        if(payInType.getTypeName() == null || "".equals(payInType.getTypeName().trim())){
            return BaseResult.body(payInType,ResultType.BUSINESS_ERROR).withMessage("属性名称为空");
        }
        if(payInType.getPayCode() == null || "".equals(payInType.getPayCode().trim())){
            return BaseResult.body(payInType,ResultType.BUSINESS_ERROR).withMessage("编码为空");
        }
        if(payInType.getId() == null || payInType.getId() <= 0){
            payInType.setId(KeyWorkerUtil.nextId());
            payInType.setActive(true);
            payInType.setOut(false);
            payInType.setCreatedAt(new Date());
        }
        payInType.setUpdatedAt(new Date());
        try {
            payInTypeRepository.save(payInType);
        }catch (Exception e){
            e.printStackTrace();
            return BaseResult.body(payInType,ResultType.BUSINESS_ERROR);
        }
        return BaseResult.body(payInType,ResultType.BUSINESS_SUCCESS);
    }

    /**
      * 出账入账类型配置-删除 on 2019/9/9 11:28
      *
      * @param id id
      * @return BaseResult<?>
      */
    public BaseResult<?> payinTypeDelete(Long id){
        PayInType payInType = payInTypeRepository.findById(id).orElse(null);
        if(payInType == null){
            return BaseResult.body(id,ResultType.BUSINESS_ERROR).withMessage("该数据不存在");
        }
        try {
            payInTypeRepository.delete(payInType);
        }catch (Exception e){
            e.printStackTrace();
            return BaseResult.body(id,ResultType.BUSINESS_ERROR);
        }
        return BaseResult.body(id,ResultType.BUSINESS_SUCCESS);
    }

    /**
      * 出账入账类型配置-启用禁用 on 2019/9/9 12:12
      *
      * @param id id
      * @return BaseResult<?>
      */
    public BaseResult<?> payinTypeUpdateActive(Long id){
        PayInType payInType = payInTypeRepository.findById(id).orElse(null);
        if(payInType == null){
            return BaseResult.body(id,ResultType.BUSINESS_ERROR).withMessage("该数据不存在");
        }
        if(payInType.isActive()){
            payInType.setActive(false);
        }else{
            payInType.setActive(true);
        }
        try {
            payInTypeRepository.save(payInType);
        }catch (Exception e){
            e.printStackTrace();
            return BaseResult.body(id,ResultType.BUSINESS_ERROR);
        }
        return BaseResult.body(id,ResultType.BUSINESS_SUCCESS);
    }
}